home *** CD-ROM | disk | FTP | other *** search
/ PC User 2002 March / Disc 1 / PCU030201.iso / software / utils / files / activesms.exe / data1.cab / Example_Files / Samples / CPP / CPPDemoDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-19  |  6.6 KB  |  249 lines

  1. // CPPDemoDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "CPPDemo.h"
  6. #include "CPPDemoDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27.     // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CAboutDlg)
  29.     protected:
  30.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31.     //}}AFX_VIRTUAL
  32.  
  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CAboutDlg)
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48.     CDialog::DoDataExchange(pDX);
  49.     //{{AFX_DATA_MAP(CAboutDlg)
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAboutDlg)
  55.         // No message handlers
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CCPPDemoDlg dialog
  61.  
  62. CCPPDemoDlg::CCPPDemoDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(CCPPDemoDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(CCPPDemoDlg)
  66.     m_sPhoneNumber = _T("44");
  67.     m_sText = _T("");
  68.     //}}AFX_DATA_INIT
  69.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  70.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71.     m_hSmallIcon = LoadImage ( AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR ); 
  72. }
  73.  
  74. void CCPPDemoDlg::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CDialog::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(CCPPDemoDlg)
  78.     DDX_Control(pDX, IDC_INCOMING_MSGS, m_lvwIncomingMsgs);
  79.     DDX_Text(pDX, IDC_PHONE_NUMBER, m_sPhoneNumber);
  80.     DDX_Text(pDX, IDC_TEXT, m_sText);
  81.     DDX_Control(pDX, IDC_ACTIVESMSLISTENER1, m_ActiveSMSListener);
  82.     //}}AFX_DATA_MAP
  83. }
  84.  
  85. BEGIN_MESSAGE_MAP(CCPPDemoDlg, CDialog)
  86.     //{{AFX_MSG_MAP(CCPPDemoDlg)
  87.     ON_WM_SYSCOMMAND()
  88.     ON_WM_PAINT()
  89.     ON_WM_QUERYDRAGICON()
  90.     ON_BN_CLICKED(IDC_SENDMSG, OnSendMsg)
  91.     //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CCPPDemoDlg message handlers
  96.  
  97. BOOL CCPPDemoDlg::OnInitDialog()
  98. {
  99.     CDialog::OnInitDialog();
  100.  
  101.     // Add "About..." menu item to system menu.
  102.  
  103.     // IDM_ABOUTBOX must be in the system command range.
  104.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  105.     ASSERT(IDM_ABOUTBOX < 0xF000);
  106.  
  107.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  108.     if (pSysMenu != NULL)
  109.     {
  110.         CString strAboutMenu;
  111.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  112.         if (!strAboutMenu.IsEmpty())
  113.         {
  114.             pSysMenu->AppendMenu(MF_SEPARATOR);
  115.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  116.         }
  117.     }
  118.  
  119.     // Set the icon for this dialog.  The framework does this automatically
  120.     //  when the application's main window is not a dialog
  121.     SetIcon(m_hIcon, TRUE);            // Set big icon
  122.     SetIcon((HICON)m_hSmallIcon, FALSE);        // Set small icon
  123.     
  124.     // TODO: Add extra initialization here
  125.     m_lvwIncomingMsgs.InsertColumn ( 0, "Phone Number", LVCFMT_LEFT, 110 );
  126.     m_lvwIncomingMsgs.InsertColumn ( 1, "Text", LVCFMT_LEFT, 130 );
  127.     m_lvwIncomingMsgs.InsertColumn ( 2, "Handset", LVCFMT_LEFT, 70 );
  128.  
  129.     m_ActiveSMS.CreateInstance ( __uuidof(ActiveSMS) );
  130.     if ( !(bool)m_ActiveSMS )
  131.     {
  132.         ::AfxMessageBox ( "Could not create ActiveSMS Com Component" );
  133.     }
  134.  
  135.     //Listener should be enabled after initialisation to prevent loss of messages
  136.     m_ActiveSMSListener.SetEnabled ( TRUE );
  137.  
  138.     return TRUE;  // return TRUE  unless you set the focus to a control
  139. }
  140.  
  141. void CCPPDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
  142. {
  143.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  144.     {
  145.         CAboutDlg dlgAbout;
  146.         dlgAbout.DoModal();
  147.     }
  148.     else
  149.     {
  150.         CDialog::OnSysCommand(nID, lParam);
  151.     }
  152. }
  153.  
  154. // If you add a minimize button to your dialog, you will need the code below
  155. //  to draw the icon.  For MFC applications using the document/view model,
  156. //  this is automatically done for you by the framework.
  157.  
  158. void CCPPDemoDlg::OnPaint() 
  159. {
  160.     if (IsIconic())
  161.     {
  162.         CPaintDC dc(this); // device context for painting
  163.  
  164.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  165.  
  166.         // Center icon in client rectangle
  167.         int cxIcon = GetSystemMetrics(SM_CXICON);
  168.         int cyIcon = GetSystemMetrics(SM_CYICON);
  169.         CRect rect;
  170.         GetClientRect(&rect);
  171.         int x = (rect.Width() - cxIcon + 1) / 2;
  172.         int y = (rect.Height() - cyIcon + 1) / 2;
  173.  
  174.         // Draw the icon
  175.         dc.DrawIcon(x, y, m_hIcon);
  176.     }
  177.     else
  178.     {
  179.         CDialog::OnPaint();
  180.     }
  181. }
  182.  
  183. // The system calls this to obtain the cursor to display while the user drags
  184. //  the minimized window.
  185. HCURSOR CCPPDemoDlg::OnQueryDragIcon()
  186. {
  187.     return (HCURSOR) m_hIcon;
  188. }
  189.  
  190. void CCPPDemoDlg::OnSendMsg() 
  191. {
  192.     UpdateData();
  193.  
  194.     try
  195.     {
  196.         int nMessageId = m_ActiveSMS->SendMessage ( (LPCSTR)m_sPhoneNumber, (LPCSTR)m_sText, 15000/*15secs*/ );
  197.         EnumJobStatus eStatus = m_ActiveSMS->GetSendStatus ( nMessageId );
  198.  
  199.         switch ( eStatus )
  200.         {
  201.             case jsSent :
  202.                 AfxMessageBox ( "Message has been sent" );
  203.                 break;
  204.             case jsErrTimeout :
  205.                 AfxMessageBox ( "Message not sent, Server too busy [Non-Queued Mode]" );
  206.                 break;
  207.             case jsErrNumberInvalid :
  208.                 AfxMessageBox ( "Message not sent, Invalid Number" );
  209.                 break;
  210.             case jsPending :
  211.             case jsProcessing :
  212.                 AfxMessageBox ( "Message has been queued" );
  213.                 break;
  214.             case jsErrNetworkFailure :
  215.                 AfxMessageBox ( "Message not sent, Failure reported by phone network" );
  216.                 break;
  217.             case jsErrPhoneCommsError :
  218.                 AfxMessageBox ( "Message not sent, Error communicating with handset" );
  219.                 break;
  220.             default :
  221.                 AfxMessageBox ( "Message not sent, Internal error occured (see EventLog)" );
  222.                 break;
  223.         }
  224.     }
  225.     catch ( _com_error & e )
  226.     {
  227.         AfxMessageBox ( CString("Error occurred") + (LPCSTR)e.Description() );
  228.     }
  229.     catch ( ... )
  230.     {
  231.         AfxMessageBox ( "Unknown Exception occurred" );
  232.     }
  233. }
  234.  
  235. BEGIN_EVENTSINK_MAP(CCPPDemoDlg, CDialog)
  236.     //{{AFX_EVENTSINK_MAP(CCPPDemoDlg)
  237.     ON_EVENT(CCPPDemoDlg, IDC_ACTIVESMSLISTENER1, 1 /* OnMessageReceived */, OnMessageReceived, VTS_BSTR VTS_BSTR VTS_I4)
  238.     //}}AFX_EVENTSINK_MAP
  239. END_EVENTSINK_MAP()
  240.  
  241. void CCPPDemoDlg::OnMessageReceived(LPCTSTR PhoneNumber, LPCTSTR Message, long Handset) 
  242. {
  243.     char Buffer[50];
  244.  
  245.     int nItemIdx = m_lvwIncomingMsgs.InsertItem ( m_lvwIncomingMsgs.GetItemCount(), PhoneNumber );
  246.     m_lvwIncomingMsgs.SetItemText ( nItemIdx, 1, Message );
  247.     m_lvwIncomingMsgs.SetItemText ( nItemIdx, 2, itoa(Handset,Buffer,10) );
  248. }
  249.